﻿using CommunityToolkit.Mvvm.Messaging;
using GameView.Comment;
using GameView.Services;
using GameView.Services.Comment;
using GameView.ViewModels;
using GameView.Views;
using MaterialDesignThemes.Wpf;
using Microsoft.Extensions.DependencyInjection;

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Windows;

namespace GameView
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static IServiceProvider ServiceProvider { get; private set; }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            var tcp = ExtentHelper.GetService<HPSocketTcpClientService>();
            if (tcp == null)
                return;
            tcp.Disconnect();
            tcp.Dispose();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            CultureInfo culture = new CultureInfo("zh-CN"); // 或 "en-US", "fr-FR" 等
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;

            // 配置DI容器
            var services = new ServiceCollection();
            services.AddSingleton<IMessenger, WeakReferenceMessenger>();
            services.AddDataProtection();

            services.AddSingleton<IDialogService, DialogService>();
            services.AddTransient<GlobalMessage>();

            services.AddTransient<StatisticsService>();
            services.AddTransient<CommandService>();
            services.AddTransient<CommandRecordService>();
            services.AddTransient<BlacklistService>();
            services.AddTransient<SysLogService>();
            services.AddTransient<RechargeToMService>();
            services.AddSingleton<RoleService>();
            services.AddSingleton<UserService>();
            services.AddSingleton<RechargeService>();
            services.AddSingleton<ProjectService>();
            services.AddSingleton<HPSocketTcpClientService>();
            services.AddSingleton<AuthService>();
            services.AddSingleton<MenuService>();
            services.AddSingleton<WithdrawalService>();
            services.AddTransient<ConsumProductService>();
            services.AddTransient<GameLoginService>();

            services.AddTransient<StatisticsByMeViewModel>();
            services.AddTransient<StatisticsByMeView>();

            services.AddTransient<ReSetPasswordViewModel>();
            services.AddTransient<ReSetPasswordView>();

            services.AddTransient<BlacklistView>();
            services.AddTransient<BlacklistViewModel>();

            services.AddTransient<BlacklistAddView>();
            services.AddTransient<BlacklistAddViewModel>();

            services.AddTransient<SysLogViewModel>();
            services.AddTransient<SysLogView>();

            services.AddTransient<RechargeToMViewModel>();
            services.AddTransient<RechargeToMView>();

            services.AddTransient<StatisticsByProjectsViewModel>();
            services.AddTransient<StatisticsByProjectsView>();

            services.AddTransient<AssignProjectView>();
            services.AddTransient<AssignProjectViewModel>();

            services.AddTransient<FastRehcargeView>();
            services.AddTransient<FastRechargeViewModel>();

            services.AddTransient<RechargeAddToMeViewModel>();
            services.AddTransient<RechargeAddToMeView>();

            services.AddTransient<UserViewModel>();
            services.AddTransient<UserView>();
            services.AddTransient<UserAddNewViewModel>();
            services.AddTransient<UserAddNew>();

            services.AddTransient<PeojectView>();
            services.AddTransient<ProjectViewModel>();

            services.AddTransient<ConProStatisticsView>();
            services.AddTransient<ConProStatisticsViewModel>();

            services.AddTransient<RechargeView>();
            services.AddTransient<RechargeViewModel>();

            services.AddTransient<RechargeAddNewView>();
            services.AddTransient<RechargeAddNewViewModel>();

            services.AddTransient<WithdrawalView>();
            services.AddTransient<WithdrawalViewModel>();
            services.AddTransient<WithdrawalUpdateView>();

            services.AddTransient<MainViewModel>();
            services.AddTransient<LoginViewModel>();
            services.AddTransient<LoginView>();
            services.AddTransient<CommandRecordViewModel>();
            services.AddTransient<CommandRecordView>();
            services.AddTransient<CommandRecordAddView>();
            services.AddTransient<CommandRecordAddViewModel>();
            services.AddTransient<CommandAddView>();
            services.AddTransient<CommandAddViewModel>();
            services.AddTransient<MainWindow>();

            services.AddTransient<ConsumProductViewModel>();
            services.AddTransient<ConsumProductView>();
            services.AddTransient<GameLoginViewModel>();
            services.AddTransient<GameLoginView>();

            ServiceProvider = services.BuildServiceProvider();
            services.Configure<JsonSerializerOptions>(options =>
            {
                options.PropertyNameCaseInsensitive = true;
                options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
            });
            // 显示登录窗口
            var loginWindow = new LoginView
            {
                DataContext = ServiceProvider.GetRequiredService<LoginViewModel>()
            };
            loginWindow.Show();
        }

        private static SnackbarMessageQueue _messageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3));

        public static void ShowMessage(string message)
        {
            _messageQueue.Enqueue(message);
        }

        public static void ShowMessage(string message, string actionText, Action action)
        {
            _messageQueue.Enqueue(message, actionText, action);
        }

        public static void ShowSuccess(string message)
        {
            _messageQueue.Enqueue(new
            {
                Content = message,
                Icon = new PackIcon { Kind = PackIconKind.CheckCircle }
            });
        }

        public static void ShowError(string message)
        {
            _messageQueue.Enqueue(new
            {
                Content = message,
                Icon = new PackIcon { Kind = PackIconKind.AlertCircle }
            });
        }
    }
}